package com.liy.redisson.controller;

import com.liy.redisson.entity.Product;
import com.liy.redisson.mapper.ProductMapper;
import com.liy.redisson.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author ly
 * @date 2022/11/7 15:18
 */

@RestController
public class OrderController {



    @Resource
    private OrderService orderService;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 乐观锁
     */
    @GetMapping("/order")
    public String order() throws InterruptedException {


        Product product = productMapper.selectById(2);
        product.setName("李四1");

//        Product product1 = productMapper.selectById(2);
//        product1.setName("李四2");
//
//        productMapper.updateById(product1);
        int i = productMapper.updateById(product);
        if (i > 0){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }

        //JET 获取用户id

        return "";
    }


    /**
     * 测试SpringBoot事务
     */

    @Transactional   //从本方式出发得，所有事务只要有一处抛出异常就是全部进行回滚  异步也进行回滚
    @GetMapping("manage")
    public void manage(){

        Product product = productMapper.selectById(2);
        product.setName("修改成功");
        orderService.update(product);
        any();
        man();
    }


    public void man(){
        Product product = productMapper.selectById(1);
        product.setName("你好");
        orderService.update(product);
        int i = 10 /0;
    }

    @Async
    public void any(){
        Product product = new Product();
        product.setName("张三");
        product.setPrice(100);
        productMapper.insert(product);
    }
}
